package com.yb.xczx.cloud.auth.generator;

import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenContext;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;

import java.time.Instant;
import java.util.Base64;
import java.util.UUID;

@Slf4j
public final class XczxOAuth2RefreshTokenGenerator implements OAuth2TokenGenerator<OAuth2RefreshToken> {
	private final StringKeyGenerator refreshTokenGenerator =
			new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96);

	@Nullable
	@Override
	public OAuth2RefreshToken generate(OAuth2TokenContext context) {
//		if (!OAuth2TokenType.REFRESH_TOKEN.equals(context.getTokenType())) {
//			return null;
//		}
		String refreshToken = UUID.randomUUID().toString().replace("-","");
		log.info("refreshToken:{}",refreshToken);
		Instant issuedAt = Instant.now();
		Instant expiresAt = issuedAt.plus(context.getRegisteredClient().getTokenSettings().getRefreshTokenTimeToLive());
		return new OAuth2RefreshToken(refreshToken, issuedAt, expiresAt);
	}

}
